package MySQL_im;

import Warning.ShowUI;

import java.sql.*;

public class getInfo {
    public boolean found = false;
    private String url = "jdbc:mysql:// 172.19.135.251:3306/tcms?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
    private String password = "bw040914";
    private String name = "root";
    private Connection connection = null;
    private Statement statement;
    public Connection getConnection() {
        return connection;
    }
    public Statement getStatement() {
        return statement;
    }
    public getInfo(){
        try {
            connection = DriverManager.getConnection(url,name,password);
            statement = connection.createStatement();
            //System.out.println("成功");
        } catch (SQLException e) {
            System.out.println(e.toString());;
        }
    }
    public void Found(String name,String password,String pd){
        //数据库预编译操作对象获取
        String sql = "SELECT * FROM user";
        String user_name = null;
        String user_pass=null;
        ResultSet rs =null;
        try {
            rs=statement.executeQuery(sql);
            while(rs.next()){
                user_pass = rs.getString(4);
                user_name = rs.getString(1);
                if(pd.equals("登录")){
                    if(user_name.equals(name)&&user_pass.equals(password)){
                        found = true;
                        return;
                    }
                }
                if(pd.equals("注册")){
                    if(user_name.equals(name)){
                        found = true;
                        return;
                    }
                }
            }
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
    }
    public void Add(String name,String password,String sex,String birth){
        String sqlstr = "insert into user(name,password,sex,birthday) values(?,?,?,?)";
        try {
            PreparedStatement pre = connection.prepareStatement(sqlstr);
            pre.setString(1,name);
            pre.setString(2,password);
            pre.setString(3,sex);
            pre.setString(4,birth);
            pre.executeUpdate();
            new ShowUI("注册成功","欢迎使用！");
            pre.close();
            connection.close();
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
    }
    public void UpdateData(String user,int num,String type){
        String url = "UPDATE user SET "+type+"=? where name = ?";
        try {
            PreparedStatement pre = connection.prepareStatement(url);
            pre.setInt(1,num);
            pre.setString(2,user);
            pre.executeUpdate();
            pre.close();
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
    }
    public void UpdateType(String user,String ans){
        String url = "UPDATE user SET TYPE=? where name = ?";
        try {
            PreparedStatement pre = connection.prepareStatement(url);
            pre.setString(1,ans);
            pre.setString(2,user);
            pre.executeUpdate();
            pre.close();
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
    }
    public void CreateDatabase(String name){
        String cd = "create table "+name+"_user (time datetime,TYPE varchar(1000),PingHe INT,QiXu INT,YangXu INT,YinXu INT,TanShi INT,ShiRe INT,XieYu INT,QiYu INT,TeBin INT)";
        try {
            statement = connection.createStatement();
            int i=statement.executeUpdate(cd);
            if(i==0){
                System.out.println("创建表成功");
            }
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
    }
}
